package com.dtu.distributed.thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.acl.Owner;

import com.dtu.distributed.controller.BullyAlgorithmControlller;
import com.dtu.distributed.controller.TempController;
import com.dtu.distributed.enums.Action;

public class AdminElectionThread extends Thread{
	
	
	private Socket clientSocket;
	
	private DataOutputStream out;
	private DataInputStream in;
	
	public AdminElectionThread( Socket clientSocket){
		this.clientSocket=clientSocket;
		try {
			out = new DataOutputStream(clientSocket.getOutputStream());
			in = new DataInputStream(clientSocket.getInputStream());
			start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void run(){
		//	String message = new BufferedReader(new InputStreamReader(in)).readLine();
		try {
		String inMessage =in.readUTF();
		String [] message=inMessage.split(":");//action:identifier or action:identifier:newAdminTemperaturePort
		Action action = Action.getAction(message[0]);
		int nodeIdentifier=Integer.parseInt(message[1].trim());
		
		System.out.println("ELECTION MESSAGE: " + inMessage);
		switch (action) {
			case ELECTION:
				    String msg = Action.ANSWER.toString();
				    out.writeUTF(msg);
				  	System.out.println("send answer back");			
				    if(BullyAlgorithmControlller.maxIdentifier==BullyAlgorithmControlller.ownIdentifier){
//						if(!BullyAlgorithmControlller.alreadySendCoordinator){
//				    	//Send "cordinater" to all other nodes in the list with lower identifier (minus the admin node) using tcp
//						BullyAlgorithmControlller.alreadySendCoordinator=true;
						BullyAlgorithmControlller.sendCoordinatorToAll();
						//
						TempController.adminTemperatureIpAddress = TempController.myIpAddress;
						TempController.adminTemperaturePort = TempController.myadminTemperaturePort;
						TempController.makeAdmin(true);
						BullyAlgorithmControlller.electedI=BullyAlgorithmControlller.ownIdentifier;
					 //   }
					}
				    else if(!BullyAlgorithmControlller.startetElection ){
						BullyAlgorithmControlller.sendElectionToNodes();
						
					  }
					
				break;

			case COORDINATER:
				BullyAlgorithmControlller.electedI=nodeIdentifier;
				TempController.adminTemperatureIpAddress=clientSocket.getInetAddress().getHostAddress();
				TempController.adminTemperaturePort=Integer.parseInt(message[2].trim());//if message begins with coordinater is contains the new admins tempature port
				System.out.println("recieved coordinater with identifier: " + BullyAlgorithmControlller.electedI );
				TempController.makeAdmin(false);
				
				break;
			
		}
						
	
			in.close();
			clientSocket.close();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}

}
